package cn.kgc.blog.controller;

import cn.kgc.blog.pojo.Blog;
import cn.kgc.blog.pojo.User;
import cn.kgc.blog.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {
    @Resource
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    @RequestMapping("/login.do")
    public String login(String username, String password, HttpSession session, Model model){
        User user=userService.login(username,password);
        if(user!=null){
            session.setAttribute("user",user);
            return "userlist";
        }else{
            model.addAttribute("errormsg","用户名或密码错误!");
            return "login";
        }
    }
    @RequestMapping("/queryup.do")
    public String select(int id,Model model,HttpSession session){
        User user=userService.select(id);
        model.addAttribute("username",user.getUsername());
        model.addAttribute("password",user.getPassword());
//        model.addAttribute("id",user.getId());
        session.setAttribute("id",user.getId());
        return "userLogin";
    }
    @RequestMapping("/update.do")
    public String update(String username,String password,int id,Model model){
    int a=userService.update(username,password,id);
    if(a>0){
        model.addAttribute("msg","修改成功");
    }else{
        model.addAttribute("msg","修改失败");
    }
        return "userLogin";
    }
    @RequestMapping("/queryAll.do")
    public String selectAll(@RequestParam(required = false,defaultValue = "0") int pageIndex,Model model){
        if(pageIndex==0) {
            pageIndex = 1;
        }
        int pageSize=2;
        PageHelper.startPage(pageIndex,pageSize);
        List<User> users = userService.selectAll();
        PageInfo<User> info=new PageInfo<User>(users);
        model.addAttribute("info",info);
        model.addAttribute("userList",users);
        return "userquery";
    }
    @RequestMapping("/insert.do")
    public String insert(String username, String password, String nickname, int age, @RequestParam(defaultValue = "1") int sex, String mobile, String address, MultipartFile picpath,HttpServletRequest request){
        String filename=null;
        if (picpath.getSize()!= 0) {
                if(picpath.getSize()>1*1024*1024*10){
                    request.setAttribute("errormsg","文件大小不能超过1M");
                    return "add";
                }
                //获取上传路径
                String realPath = request.getServletContext().getRealPath("images" + File.separator + "upload");
                //获取原文件名

                String originalFilename = picpath.getOriginalFilename();
                //获取原文件拓展名
                String extension = FilenameUtils.getExtension(originalFilename);
                System.out.println(extension);
                List<String>  list=new ArrayList<>();
                list.add("jpg");
                list.add("JPG");
                list.add("png");
                list.add("PNG");
                if(list.contains(extension)==false){
                    request.setAttribute("errormsg","文件类型必须是jpg或png");
                    return "add";
                }
                //生成新的文件名
                 filename = System.currentTimeMillis() + RandomUtils.nextInt(1000000) + "upload.jpg";
                System.out.println(filename);
                //封装成File对象
                File file = new File(realPath, filename);
                //上传
                try {
                    picpath.transferTo(file);

                } catch (IOException e) {

                }
            }
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setNickname(nickname);
        user.setAge(age);
        user.setSex(sex);
        user.setMobile(mobile);
        user.setAddress(address);
        user.setPicpath(filename);
        int insert = userService.insert(user);
        if(insert>0){
            request.setAttribute("msg","添加成功");
            return "add";
        }else{
            request.setAttribute("msg","添加失败");
            return "add";
        }
    }
    @RequestMapping("/delete.do")
    public String delete(int id,Model model){
        int delete = userService.delete(id);
        if(delete>0){
            model.addAttribute("msg","删除成功");
        }else{
            model.addAttribute("msg","删除失败");
        }
        return "redirect:/queryAll.do";
    }
}
